package com.ebt.m.utils;

import android.app.Activity;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by Rick.Wang on 2016/12/1.
 */
public class SharedElementUtil {

    public static void postponeEnterTransition(Activity activity){
        ActivityCompat.postponeEnterTransition(activity);
    }

    public static void startPostponeEnterTransition(Activity activity){
        ActivityCompat.startPostponedEnterTransition(activity);
    }

    /**
     * 当view解析完成之后，开始动画
     * @param view
     */
    public static void startPostponeEnterTransition(View view){
        if (view.getContext() instanceof Activity) {
            view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    view.getViewTreeObserver().removeOnPreDrawListener(this);
                    ActivityCompat.startPostponedEnterTransition((Activity) view.getContext());
                    return true;
                }
            });
        }
    }

    /**
     * 从界面 Activity A跳转到 Activity B时，B在onCreate中添加此函数
     * @param activity
     * @param callback
     */
    public static void setEnterSharedElementCallback(Activity activity, SharedElementCallback callback){
        if (Build.VERSION.SDK_INT >= 22) {
            activity.setEnterSharedElementCallback(callback);
        }
    }

    /**
     * 从界面 Activity A跳转到 Activity B时，A在onCreate中添加此函数
     * @param activity
     * @param callback
     */
    public static void setExitSharedElementCallback(Activity activity, SharedElementCallback callback){
        if (Build.VERSION.SDK_INT >= 22) {
            activity.setExitSharedElementCallback(callback);
        }
    }

    public static void startDetailActivity(Activity activity, Intent intent, View view, String sharedElementKey) {
        ActivityOptionsCompat options = ActivityOptionsCompat
                .makeSceneTransitionAnimation(activity, view, sharedElementKey);
        ActivityCompat.startActivity(activity, intent, options.toBundle());
    }

    //在A界面添加如下函数
    /*
    @TargetApi(22)
    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
        supportPostponeEnterTransition();
        reenterState = new Bundle(data.getExtras());
        mRecyclerView.scrollToPosition(reenterState.getInt(INDEX, 0));
        mRecyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                mRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
                mRecyclerView.requestLayout();
                supportStartPostponedEnterTransition();
                return true;
            }
        });
    }
    */

    //在B界面添加如下函数
    /*
    @TargetApi(22)
    @Override
    public void supportFinishAfterTransition() {
        Intent data = new Intent();
        data.putExtra("index", mPager.getCurrentItem());
        setResult(RESULT_OK, data);
        super.supportFinishAfterTransition();
    }
    */

    /*if (Build.VERSION.SDK_INT >= 22) {
        //这个在退出的时候调用  把当前的共享元素传递到后面的activity
        setExitSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                if (reenterState != null) {
                    int i = reenterState.getInt(INDEX, 0);
                    sharedElements.clear();
                    sharedElements.put(mAdapter.get(i).getUrl(), mLayoutManager.findViewByPosition(i));
                    reenterState = null;
                }
            }
        });
    }*/

    /*if (Build.VERSION.SDK_INT >= 22) {
        //这个可以看做个管道  每次进入和退出的时候都会进行调用  进入的时候获取到前面传来的共享元素的信息
        //退出的时候 把这些信息传递给前面的activity
        //同时向sharedElements里面put view,跟对view添加transitionname作用一样
        activity.setEnterSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                String url = urls.get(mPager.getCurrentItem());
                LargePicFragment fragment = (LargePicFragment) adapter.instantiateItem(mPager, mPager.getCurrentItem());
                sharedElements.clear();
                sharedElements.put(url, fragment.getSharedElement());
            }
        });
    }*/
}
